package edu.hncj.seckill.controller;

import com.google.common.util.concurrent.RateLimiter;
import edu.hncj.seckill.domain.Order;
import edu.hncj.seckill.domain.Product;
import edu.hncj.seckill.response.Result;
import edu.hncj.seckill.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@CrossOrigin
@Controller
@RequestMapping("/order")
public class OrderController {


    @Autowired
    OrderService orderService;

    // 桶令牌的设置， 1 是指产生一个令牌，1000 是指1000秒，即1000秒产生一个令牌
//    private RateLimiter rateLimiter = RateLimiter.create(1,1000,TimeUnit.SECONDS);

    // 桶令牌的设置每次一次产生100个
    private RateLimiter rateLimiter = RateLimiter.create(1000);

    /**
     * 用户购买接口
     * @param uid 买家ID
     * @param pid 商品ID
     * @return 返结果
     */
    @ResponseBody
    @RequestMapping(value="/shop")
    public Result orderShop(@RequestParam("uid")String uid, @RequestParam("pid")String pid){
        // 创建返回对象
        Map<String,Object> map = new HashMap<>(2);
        try {
            if(!rateLimiter.tryAcquire(1, TimeUnit.SECONDS)){
                map.put("code", 34);
                map.put("msg", "抢购火爆，请稍后再试！");
                return Result.create(map);
            }
            try {
                return doOrderShop(uid, pid);
            }catch (Exception e){
                e.printStackTrace();
                map.put("code", 35);
                map.put("msg", "系统异常！");
                return Result.create(map);
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 35);
            map.put("msg", "系统异常！");
            return Result.create(map);
        }
    }


    private Result doOrderShop(String uid,String pid){
        // 创建返回对象
        Map<String,Object> map = new HashMap<>(2);
        // 第一步先根据商品ID查询商品对象
        Product product = orderService.selectProductByPid(pid);
        // 判断是否该商品已经售空,如果已经售空就直接返回提示
        if(product.getSale().equals(product.getCount())){
            map.put("code", 30);
            map.put("msg", "该商品已经售空");
            return Result.create(map);
        }
        // 第二步扣除库存
        int b = orderService.updateProduct(product);
        if (b == 0){
            map.put("code", 31);
            map.put("msg", "当前服务忙！");
            return Result.create(map);
        }
        // 第三部创建订单
        Order order = orderService.createOrder(product,uid);
        if (order == null){
            map.put("code", 32);
            map.put("msg", "订单异常，请联系客服人员！");
            return Result.create(map);
        }
        // 流程走完了
        map.put("code", 33);
        map.put("order", order);
        map.put("msg", "抢购成功，正在跳转付款中！");
        return Result.create(map);
    }

}
